<template>
  <div class="app-container static-container">
    <div class="content-wrap">
      <el-form
        :model="queryParams"
        size="mini"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="作者" prop="author" label-width="40px">
          <el-input
            v-model="queryParams.author"
            placeholder="请输入作者"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="投稿用户" prop="loginName">
          <el-input
            v-model="queryParams.loginName"
            placeholder="请输入投稿用户"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="投稿部门" prop="deptName">
          <el-input
            v-model="queryParams.deptName"
            placeholder="请输入投稿部门"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
            align="right"
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-form-item>
            <el-badge
              :value="$store.state.statistics.channelIds.length"
              :max="10"
              class="item"
            >
              <el-button type="primary" size="mini" @click="openChannel1"
                >选择栏目</el-button
              >
            </el-badge>
          </el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
          <el-button
            icon="el-icon-back"
            size="mini"
            @click="goback"
            v-if="false"
            >返回</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="loading" :data="articleDetailList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column
        label="文章标题"
        align="center"
        prop="header"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="getArticleUrl(scope.row)">{{
            scope.row.header
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="作者" align="center" prop="author1" width="100" />
      <el-table-column
        label="栏目名称"
        align="center"
        prop="channelName"
        width="100"
      />
      <el-table-column
        label="部门"
        align="center"
        prop="deptName"
        width="100"
      />
      <el-table-column
        label="投稿用户"
        align="center"
        prop="loginName"
        width="100"
      />
      <el-table-column
        label="发布时间"
        align="center"
        prop="createdDate"
        width="200"
      >
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { articleViewUrl } from "@/api/content/mywork";
import { listArticleDetail } from "@/api/statistics/statistics";
import { mapState } from "vuex";
import { objectMerge } from "@/utils/index";
export default {
  name: "Article_detail",

  data() {
    return {
      // 遮罩层
      loading: true,
      pickerOptions: {
        shortcuts: [
          {
            text: "本月",
            onClick(picker) {
              const start = new Date();
              start.setDate(1);

              picker.$emit("pick", [start, new Date()]);
            },
          },
          {
            text: "今年至今",
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近六个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
        disabledDate(time) {
          //time = new Date()
          return time.getTime() > Date.now(); // 可选历史天、可选当前天、不可选未来天
          // return time.getTime() > Date.now() - 8.64e7;  // 可选历史天、不可选当前天、不可选未来天
          // return time.getTime() < Date.now() - 8.64e7;  // 不可选历史天、可选当前天、可选未来天
          // return time.getTime() < Date.now(); // 不可选历史天、不可选当前天、可选未来天
        },
      },
      // 总条数
      total: 0,
      // 文章详情表格数据
      articleDetailList: [],

      // 类型数据字典
      statusOptions: [],
      // 状态数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        author: undefined,
        deptName: undefined,
        loginName: undefined,
      },
    };
  },

  components: {},

  computed: {
    ...mapState({
      siteRow: (state) => state.column.siteRow,
      queryVuex: (state) => state.statistics.query,
      dateRangeVuex: (state) => state.statistics.dateRange,
      channelIdsVuex: (state) => state.statistics.channelIds,
    }),
  },
  watch: {
    value: {
      handler(val) {},
      deep: true,
      immediate: true,
    },
  },

  mounted() {
    this.dateRange = this.dateRangeVuex;
    this.queryParams.siteId = this.siteRow.siteId;
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      this.queryParams = {
        ...this.queryParams,
        ...this.queryVuex,
        channelIds: this.channelIdsVuex.join(","),
      };
      console.log(
        "this.queryParams, this.dateRange: ",
        this.queryParams,
        this.dateRange
      );

      listArticleDetail(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((res) => {
        this.articleDetailList = res.data.list;
        this.total = res.data.totalCount;
        this.loading = false;
      });
    },
    goback() {
      // this.$router.go(-1);
      this.$emit("changeComponent", "home");
    },
    //文章标题跳转文章页面
    getArticleUrl(row) {
      articleViewUrl({
        articleId: row.articleId,
      }).then((res) => {
        let href = res.msg;
        window.open(href, "_blank");
      });
    },
    openChannel1() {
      this.$emit("openChannel", 111);
      return;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        author: undefined,
        deptName: undefined,
        loginName: undefined,
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$store.dispatch("statistics/setStaticsQuery", {
        loginName: this.queryParams.loginName,
        author: this.queryParams.author,
        deptName: this.queryParams.deptName,
      });
      this.$store.dispatch("statistics/setDateRange", this.dateRange);

      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.$store.dispatch("statistics/setStaticsQuery", {});
      this.$store.dispatch("statistics/setChannelIds", []);
      this.$store.dispatch("statistics/setDateRange", []);

      this.handleQuery();
    },
  },
};
</script>

<style scoped lang="scss"></style>
